package com.example.mysendingtheuser;

import java.util.List;

import org.apache.http.protocol.HTTP;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MySendingTheUser extends Activity {
	public Button call_number,view_a_map,view_a_webpage,send_an_email,choose_app;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_sending_the_user);
		setView();
	}
	
	private void setView(){
		call_number = (Button)findViewById(R.id.call_number);
		call_number.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				callNumber("tel:5551234");
			}
		});
		
		view_a_map = (Button)findViewById(R.id.view_a_map);
		view_a_map.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				viewMap();
			}
		});
		
		view_a_webpage = (Button)findViewById(R.id.view_a_webpage);
		view_a_webpage.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				viewWebpage("http://www.android.com");
			}
		});
		
		send_an_email = (Button)findViewById(R.id.send_an_email);
		send_an_email.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				sendEmail();
			}
		});
		
		choose_app = (Button)findViewById(R.id.choose_app);
		choose_app.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				
			}
		});
	}
	
	private void callNumber(String num){
		Uri number = Uri.parse(num);
		Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
		if(checkIntentSafe(callIntent))
			startActivity(callIntent);
	}
	
	private void viewMap(){
		// Map point based on address
		Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
		// Or map point based on latitude/longitude
		// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level
		Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
		if(checkIntentSafe(mapIntent))
			startActivity(mapIntent);
	}
	
	private void viewWebpage(String url){
		Uri webpage = Uri.parse(url);
		Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
		if(checkIntentSafe(webIntent))
			startActivity(webIntent);
	}
	
	private void sendEmail(){
		Intent emailIntent = new Intent(Intent.ACTION_SEND);
		
		showChooseApp(emailIntent);
		// The intent does not have a URI, so declare the "text/plain" MIME type
		emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
		emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); // recipients
		emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
		emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
		emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
		// You can also attach multiple items by passing an ArrayList of Uris
		if(checkIntentSafe(emailIntent))
			startActivity(emailIntent);
	}
	
	private boolean checkIntentSafe(Intent intent){
		PackageManager packageManager = getPackageManager();
		List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
		boolean isIntentSafe = activities.size() > 0;
		return isIntentSafe;
	}
	
	private void showChooseApp(Intent intent){
		// Always use string resources for UI text. This says something like "Share this photo with"
		String title = (String) getResources().getText(R.string.chooser_title);
		// Create and start the chooser
		Intent chooser = Intent.createChooser(intent, title);
		startActivity(chooser);
	}
}
